/*
 * dbm_lsdb_handle.c
 *
 *  Created on: 2012-12-14
 *      Author: macro
 */
#include "cmn/common.h"
#include "cmn/utils.h"
#include "cmn/util.h"
#include "dbm_conf.h"
#include "lsdb/dbm_lsdb_handle.h"
#include "lsdb/dbm_lsdb_manager.h"
#include "lsdb/lsdb.h"
#include "timer/timer.h"
#include "server/dbm_server.h"
char global_snapshoot[MAX_LSDB_SIZE];
int global_snapshoot_len;


int dbm_lsdb_handle(u_char* data,int len){
	//get packet data
	//get method
	//swithc method
	//add? update? send?
	printf("Begin handle\n");
	struct lsdb_msgheader head;
	memcpy(&head,data,sizeof(struct lsdb_msgheader));
	int area_id = head.area_id;
	int pkt_len = head.length;
	int action = head.action;
	printf("area[%d],pktLen[%d],action[%d]\n",area_id,pkt_len,action);
	struct link_state_pkt* lsa = (struct link_state_pkt*)malloc(sizeof(struct link_state_pkt));
	//struct link_state_key key;
	//char * msg = (char* )malloc(100);
	memcpy(lsa,data+4,pkt_len);
	//printf("%d %d %d %d\n",lsa->rt_id,lsa->if_id,lsa->n_rt_id,lsa->n_if_id);
	//printf("sizeof(ls):%d\n",sizeof(struct cr_lsdb_link_state));
	struct cr_lsdb_link_state* ls = malloc_z(struct cr_lsdb_link_state);
	ls->key.rt_id = lsa->rt_id;
	ls->key.if_id = lsa->if_id;
	ls->key.n_rt_id = lsa->n_rt_id;
	ls->key.n_if_id = lsa->n_if_id;
	ls->metric = lsa->metric;
	ls->seq = lsa->seq;
	ls->state = lsa->state;
	show_ls(ls);
	//show_ls_pkt(lsa);
	switch(action){
	case ADD_LSA: add_link_state_to_area(ls,area_id);
	break;
	case UPDATE_LSA: update_link_state_to_area(ls,area_id);
	break;
	default:break;
	}
	//add_link_state_to_area(ls,0);
	//printf("hello,world\n");
	//printf("%s\n",(data+4));
}
